package com.tr.shopping.product.service.impl;

import com.tr.shopping.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tr.common.utils.PageUtils;
import com.tr.common.utils.Query;

import com.tr.shopping.product.dao.CategoryDao;
import com.tr.shopping.product.entity.CategoryEntity;
import com.tr.shopping.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查出所有分类以及子分类列表 以树形结构显示
     */
    @Override
    public List<CategoryEntity> getListCategoryForTree() {
        //查询出所有的目录
        List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);

        //获取父目录
        List<CategoryEntity> levellMenus = categoryEntityList.stream().filter(categoryEntity -> categoryEntity.getParentCid().longValue()==0)
                .map((menu)->{
                    menu.setChildren(getChildrens(menu,categoryEntityList));
                    return menu;
                }).sorted((menu1,menu2)->{
                    return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
                }).collect(Collectors.toList());


        return levellMenus;
    }

    @Override
    public void removeByListId(List<Long> asList) {
        //TODO 1检查当前删除菜单 是否被其他地方应用 当其他引用时 不能删除

        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths=new ArrayList<>();
        List<Long> parentNodes = findParentNodes(catelogId, paths);
        //数据翻转 3 2 1 1 2 3
        Collections.reverse(parentNodes);
        return (Long[]) parentNodes.toArray(new Long[parentNodes.size()]);
    }

    @Transactional   //开启事务机制
    @Override
    public void updateInfamation(CategoryEntity category) {
        this.updateById(category);
        //同级更新
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    private List<Long> findParentNodes(Long catelogId,List<Long> paths){
        //收集当前节点 id
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if(byId.getParentCid()!=0){
            findParentNodes(byId.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children=all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().longValue()==root.getCatId().longValue();
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            return  (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}